const app = getApp();
import { Category } from '../../pages/category/category-model.js';
var category = new Category();
Component({
  properties: {
  },
  data: {
    show_type: 1,
    categoryData: [],
    loadingHidden: false,
    currentMenuIndex: 0,
    hiddenHis:true
  },

  ready: function () {
    this._loadData();
  },
  methods: {
    init: function () {
      this.setData({
        loadingHidden: true
      })
    },
    /*加载所有类目数据*/
    _loadData: function (callback) {
      var that = this;
      var param = {
        num: 100000,
        shop_id: 1
      }
      category.getproductCategoryData(param, (data) => {
        this.setData({
          categoryData: data.items,
        })
      });
    },
    /*切换分类*/
    changeCategory: function (event) {
      var that = this,
      index = category.getDataSet(event, 'index');

      this.data.pid = event.currentTarget.dataset.id;
      this.data.type_id = '';
      that.setData({
        currentMenuIndex: index,
        sonIndex: '',
        toView: "category_id_" + that.data.categoryData[index].category_id
      });
      if (!that.data.categoryData[index].sons){
        this.sendData({ id: that.data.categoryData[index].category_id, name: that.data.categoryData[index].cat_name });
      }
    },
    //选中类目
    select: function (e) {
      var id = e.currentTarget.dataset.id;
      var name = e.currentTarget.dataset.name;
      this.sendData({ id: id, name: name });
    },
    //传递参数给父级
    sendData: function (param) {
      this.triggerEvent('getCate', param);
      this.close();
    },
    close: function () {
      this.setData({
        loadingHidden: false
      })
    },
    /*搜索*/
    showInput: function () {
      this.setData({
        inputShowed: true,
        show_search: 1
      });
    },
    clearInput: function () {
      this.setData({
        inputVal: ""
      });
    },
    inputTyping: function (e) {
      this.data.inputVal = e.detail.value,
        this.setData({
          inputShowed: true
        });
    },
    searchBtn: function (e) {
      var rs = this.search(this.data.inputVal);
      console.log(rs);
      this.setData({
        history: rs,
        hiddenHis: false

      })
    },
    closeHistory: function () {
      this.setData({
        hiddenHis: true
      })
    },
    search: function (name) {
      var newList = [];
      var data = this.data.categoryData;
      for (var i = 0; i < data.length; i++) {
        var obj = data[i];
        if (obj.cat_name.indexOf(name) > -1) {
          newList.push(obj);
        }
        if (!obj.sons) {
          continue;
        }
        var childs = obj.sons
        for (var j = 0; j < childs.length; j++) {
          var child = childs[j];
          if (child.cat_name.indexOf(name) > -1) {
            child.father = obj.cat_name;
            newList.push(child);
          }
          if (!child.sons) {
            continue;
          }
          var sons = child.sons;
          for (var k = 0; k < sons.length; k++) {
            var son = sons[k];
            if (son.cat_name.indexOf(name) > -1) {
              son.grandfather = obj.cat_name;
              son.father = child.cat_name;
              newList.push(son);
            }
          }
        }
      }
      return newList;

    }
  }
})